package com.wanmao.test;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;


import com.wanmao.test.util.PerferenceUntil;
import com.wanmao.test.util.StaticUtil;

import java.util.ArrayList;
import java.util.List;

import static com.wanmao.test.util.ToolUtils.getCheckTime;
import static com.wanmao.test.util.ToolUtils.setCheckTime;


public class SettingActivity extends BaseActivity implements AdapterView.OnItemSelectedListener {

    private Spinner spinner;
    private List<String> list;

    private ArrayAdapter<String> adapter;
    private int i;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        intenView();
    }

    private void intenView() {
        SharedPreferences sp = this.getSharedPreferences("time", this.MODE_PRIVATE);
        StaticUtil.TIME = sp.getString("time", "");
        if (!"".equals(StaticUtil.TIME)){
            i=Integer.parseInt(StaticUtil.TIME);
        }else {
            i=3;
        }

        spinner = (Spinner) findViewById(R.id.id_spinner_time);

        list = new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        list.add("7");
        list.add("8");
        list.add("9");
        list.add("10");
        list.add("11");
        list.add("12");
        //适配器
        adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        //设置样式
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(SettingActivity.this);
        spinner.setSelection(i-1,true);
    }


    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        String time= (String) spinner.getSelectedItem();
        PerferenceUntil.shareTime(this,time);
        SharedPreferences sp = this.getSharedPreferences("time", this.MODE_PRIVATE);
        StaticUtil.TIME = sp.getString("time", "");
        int share=3;
        if (!"".equals(StaticUtil.TIME)){
           share =Integer.parseInt(StaticUtil.TIME);
        }
        setCheckTime(share);
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}
